<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
      <el-form-item label="条码" prop="barcode">
      <el-input
        v-model="queryParams.barcode"
        placeholder="请输入条码"
        clearable
        @keyup.enter.native="handleQuery"
      />
    </el-form-item>
      <el-form-item label="容器编号" prop="containerNo">
        <el-input
          v-model="queryParams.containerNo"
          placeholder="请输入容器编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="库位编号" prop="storehouseLocationNo">
        <el-input
          v-model="queryParams.storehouseLocationNo"
          placeholder="请输入库位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请输入类型"  >
          <el-option
            v-for="dict in dict.type.sh_inout_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="动作" prop="operation">
        <el-select v-model="queryParams.operation" placeholder="请输入动作"  >
          <el-option
            v-for="dict in dict.type.sh_inout_opr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
      <af-table-column label="物料编号" align="center" prop="productNo"  min-width="150" show-overflow-tooltip/>
      <af-table-column label="物料名称" align="center" prop="productName"  min-width="150" show-overflow-tooltip/>
      <af-table-column label="条码" align="center" prop="barcode"  min-width="100" show-overflow-tooltip/>
      <af-table-column label="容器编号" align="center" prop="containerNo"  min-width="100" show-overflow-tooltip/>
      <af-table-column label="数量" align="center" prop="quantity" />
      <af-table-column label="库位编号" align="center" prop="storehouseLocationNo"  width="120" show-overflow-tooltip/>
      <af-table-column label="工单编号" align="center" prop="workOrderNo" />
      <af-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sh_inout_type" :value="scope.row.type"/>
        </template>
      </af-table-column>
      <af-table-column label="类别" align="center" prop="operation" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sh_inout_opr" :value="scope.row.operation"/>
        </template>
      </af-table-column>
      <af-table-column label="出入库业务单号" width="180"  align="center" prop="businessNo" />
      <af-table-column label="来源单号" width="180"  align="center" prop="relNo" />
      <el-table-column label="创建人" align="center" prop="createBy" min-width="100"/>
      <el-table-column label="创建时间" align="center" prop="createTime"  width="152">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新人" align="center" prop="updateBy" min-width="100"/>
      <el-table-column label="最后更新时间" align="center" prop="createTime"  width="152">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness } from "@/api/storehouse/inoutstock/inoutstock";

export default {
  dicts: ['sh_inout_type','sh_inout_opr'],
  name: "Business",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库记录表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        batchNo: null,
        productNo: null,
        quantity: null,
        storehouseLocationNo: null,
        type: null,
        operation: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        no: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        storehouseLocationNo: [
          { required: true, message: "库位编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询出入库记录列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        batchNo: null,
        productNo: null,
        quantity: null,
        storehouseLocationNo: null,
        type: null,
        operation: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出入库记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusiness(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBusiness(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delBusiness(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/business/export', {
        ...this.queryParams
      }, `business_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
